using Zhaoxi.Zhihu.Core.AppUserAggregate.Entites;
using Zhaoxi.Zhihu.Core.AppUserAggregate.Specification;

using Zhaoxi.Zhihu.SharedKerel.Messaging;
using Zhaoxi.Zhihu.SharedKerel.Repositoy;
using Zhaoxi.Zhihu.SharedKerel.Result;
using Zhaoxi.Zhihu.UseCases.Common.Attributes;
using Zhaoxi.Zhihu.UseCases.Common.Interface;

namespace Zhaoxi.Zhihu.UseCases.AppUsers.Commands;

[Authorize]
public record DeleteFollowQuestionCommand(int QuestionId) : ICommand<IResult>;

public class DeleteFollowQuestionCommandHanlder(
    IRepository<AppUser> UserRepo,
    IUser user):ICommandHandler<DeleteFollowQuestionCommand, IResult>
{
    public async Task<IResult> Handle(DeleteFollowQuestionCommand request, CancellationToken cancellationToken)
    {
        var spec=new FollowQuestionByIdSpec(user.Id!.Value,request.QuestionId);
        var appuser= await UserRepo.GetSingleOrDefaultAsync(spec,cancellationToken);
        if (appuser == null) return Result.NotFound("用户不存在");
        appuser.RemoveFollowQuestion(request.QuestionId);
        await UserRepo.SaveChangesAsync(cancellationToken);
        return Result.Success();

    }
}

